<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    title="模具选择"
    style="min-width: 1400px"
    draggable
    @close="closeDialog"
    destroy-on-close
  >
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="模具类型" prop="assetTypeId">
          <!-- <el-input
              v-if="!props.isReadonly"
              v-model="formData.assetTypeId"
              placeholder="请输入模具类型"
              clearable
            /> -->
          <el-tree-select
            placeholder="请选择模具类型"
            v-model="searchParam.assetTypeId"
            :data="catalogList"
            node-key="id"
            :props="{
              label: 'assetTypeName',
              value: 'id',
              children: 'nodes',
            }"
            :render-after-expand="false"
            :check-strictly="true"
            clearable
          />
        </el-form-item>

        <!-- <el-form-item label="产品">
          <el-select
            v-model="searchParam.brandId"
            placeholder="请选择品牌"
            clearable
          >
            <el-option
              v-for="item in brandList"
              :key="`${item.id}`"
              :label="`${item.brandCode}-${item.brandName}`"
              :value="`${item.id}`"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="模具名称">
          <el-input
            v-model="searchParam.assetName"
            placeholder="请输入模具名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="位置">
          <el-tree-select
            placeholder="请输入位置"
            filterable
            v-model="searchParam.locationId"
            :data="locationInfoTree"
            node-key="id"
            clearable
            :props="{ label: 'locationName', children: 'nodes' }"
            check-strictly
            :render-after-expand="false"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <div class="formTable">
        <el-table
          ref="singleTableRef"
          :data="tableData"
          style="width: 100%"
          :key="Math.random"
          highlight-current-row
          @current-change="handleCurrentChange1"
          border
        >
          <el-table-column label="模具编号" prop="assetCode" min-width="50" />
          <el-table-column label="模具名称" prop="assetName" min-width="50" />
          <el-table-column
            label="品牌型号"
            prop="assetModelNo"
            min-width="50"
            align="center"
          >
            <template #default="scope">
              <span>{{ scope.row.assetModelNo }}</span>
              <span v-if="scope.row.assetModelNo">-</span>
              <span> {{ scope.row.assetBrandName }}</span>
            </template>
          </el-table-column>

          <el-table-column
            label="制造商"
            prop="makerName"
            :align="'center'"
            min-width="50"
            align="center"
          />
          <el-table-column
            label="出厂编号"
            prop="manufactureCode"
            min-width="50"
            align="center"
          />

          <el-table-column
            label="器具位置"
            prop="locationName"
            min-width="50"
            align="center"
          />
        </el-table>
      </div>
    </el-card>
    <el-pagination
      :background="true"
      :current-page="pageable.pageNum"
      :page-size="pageable.pageSize"
      :page-sizes="[5, 10, 15, 20]"
      :total="pageable.total"
      layout="total, sizes, prev, pager, next, jumper"
      class="mt-[20px]"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit"
          >确 定
        </el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import { ElMessage, ElForm } from "element-plus";
import { ref, reactive, watch, onBeforeUnmount } from "vue";
import request from "@/utils/request";
import dayjs from "dayjs";
defineOptions({
  name: "MOLDDEVICECHANGESTATUSSELECT",
  inheritAttrs: false,
});

const emit = defineEmits(["change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

//获取表格数据 api 方法
const requestApi = "/eam/mouldAssetInfo/queryByPage";
//获取数据初始化参数
const initParam = {};
// 初始化搜索参数数据
// 初始化搜索参数数据
const searchParamInit = {
  assetTypeId: "",
  assetName: "",
  brandId: "",
  locationId: "",
};
// 是否需要分页
const pagination = true;
// 无需二次数据处理&不传赞数也行
const tableData1 = ref([]);
const dataCallBack = (data: any) => {
  // data.forEach((item: any, index: number) => {
  // });
  // if (data) {
  //   data = data.filter(function (value, index, arr) {
  //     return value.assetStatus == "3";
  //   });
  // }
  return data;
};
// const dataCallBack = ref(undefined);
// 数据请求错误后处理
function requestError() {}
//删除
const deleteParam = {
  api: "",
  data: {},
};

const {
  tableData,
  pageable,
  ids,
  searchParam,
  reset,
  search,
  getTableList,
  handleSizeChange,
  handleSelectionChange,
  handleTableDelete,
  handleCurrentChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack,
  requestError,
  deleteParam
);

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

// 选择高亮
const singleTableRef = ref();
const currentRow = ref();
const handleCurrentChange1 = (val: any) => {
  currentRow.value = val;
};

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
}

/** email消息发送记录保存提交 */
function handleSubmit() {
  if (currentRow.value) {
    emit("change-visible", false, currentRow.value);
  } else {
    return ElMessage.warning("请选择工作任务");
  }
}

// 获取位置信息下拉树
const locationInfoTree = ref([]);
const getLocationInfoTree = async () => {
  const { data } = await request({
    url: "/eam/locationInfo/getTree",
    method: "POST",
    data: {
      enabled: 1,
    },
  });
  locationInfoTree.value = data;
};

// 目录树
const catalogList: any = ref([]);

// 获取内容树数据
const getCAtalogList = async () => {
  // 获取内容树
  await request({
    url: "eam/mouldAssetType/getAvailableTree",
    method: "POST",
    data: {},
  }).then((res) => {
    catalogList.value = res.data;
  });
};

// 获取资产品牌数据
const brandList: any = ref([]);
const getBrandList = async () => {
  await request({
    url: "eam/eamassetbrand/getList",
    method: "post",
    data: {},
  }).then((res) => {
    brandList.value = res.data;
  });
};
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

onMounted(() => {
  getTableList();
  // 获取位置信息下拉树
  getLocationInfoTree();
  getCAtalogList();
  getBrandList();
});
</script>

<style scoped lang="scss">
:deep(.el-table th .cell) {
  color: black;
  font-weight: 700;
}
.progress {
  color: red;
}
.success {
  color: rgb(100, 207, 86);
}
</style>
